﻿using System.Collections.Generic;
using System.Web.Mvc;
using NHibernate.Cache;
using Web7.Caching;

namespace Web7.Core.Domain.NHibernate
{
    public class Web7Cache : ICache
    {
        //private static readonly IInternalLogger Log = LoggerProvider.LoggerFor(typeof(Web7Cache));
        
        private readonly string _region;
        private readonly ICachePolicy _cachePolicy;
        private readonly ICacheManager _manager;

        public Web7Cache()
            : this("nhibernate", null)
        {
        }

        public Web7Cache(string region)
            : this(region, null)
        {
        }

        public Web7Cache(string regionName, IDictionary<string, string> properties)
        {
            _manager = CacheManager.Instance();
            _region = regionName;
            _cachePolicy = RegisterCachePolicy(properties);

        }

        #region Icache

        public object Get(object key)
        {
            return _manager.GenerationScopedCache.GetValue(CreateKey(key));
        }

        public void Put(object key, object value)
        {
             var resultKey = CreateKey(key);
             _manager.GenerationScopedCache.AddOrChange(resultKey, new CacheValueOf<object>(value, RegisterCachePolicy(null))); //todo:设置+规则
        }

        public void Remove(object key)
        {
            var resultKey = CreateKey(key);
            _manager.GenerationScopedCache.Remove(resultKey);
        }

        public void Clear()
        {
            _manager.GenerationScopedCache.Clear();
        }

        public void Destroy()
        {
            _manager.GenerationScopedCache.Clear();
        }

        public void Lock(object key)
        {

        }

        public void Unlock(object key)
        {

        }

        public long NextTimestamp()
        {
            return Timestamper.Next();
        }

        public int Timeout
        {
            get
            {
                return 0xea60000;
            }
        }

        public string RegionName
        {
            get { return _region; }
        } 
        #endregion

        /// <summary>
        /// 创建缓存KEY
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private Web7CacheKey CreateKey(object key)
        {
            return Web7CacheKey.Create(key.ToString());
        }


        #region CachePolicy

        public ICachePolicy RegisterCachePolicy(IDictionary<string, string> properties)
        {
            if (properties==null)
            {
                return StaticCachePolicy.CreateDefault();
            }
            //int se=0;
            //int priority = 0;
            //string SlidingExpiration = properties["SlidingExpiration"];
            //bool flag = int.TryParse(SlidingExpiration, out se);
            //string CacheItemPriority = properties["CacheItemPriority"];
            //flag = int.TryParse(CacheItemPriority, out priority);

            //TimeSpan
            return StaticCachePolicy.CreateDefault();  //todo:policy 待研究
        } 
        #endregion
    }
}

